import { ui } from "../../ui/layaMaxUI";
import SoundMgr from "../../core_wq/sound/SoundMgr";
import SoundType from "../../core_wq/sound/SoundType";
import GlobalData from "../../core_wq/db/GlobalData";
import HeroConfigVO from "../../core_wq/db/vo/HeroConfigVO";
import Game from "../../core_wq/game/Game";



export default class ShopView extends ui.moduleView.shop.ShopViewUI {


    _isInit = false;


    onAwake() {
        this.closeBtn.on(Laya.Event.CLICK, this, this.clickClose);
        this.buyBtn.on(Laya.Event.CLICK, this, this.ClickBuy);
        this.runShowAnim();

        this.priceLabel.text = "x" + Game.gameConfigVO.heroBuyCost;
        console.log(Game.hero.playerData)
        let heroConfigVOs: HeroConfigVO[] = GlobalData.getAllValue(GlobalData.HeroConfigVO);
        this.countLabel.text = Game.hero.playerData.heros.length + "/" + heroConfigVOs.length;

        Laya.timer.frameOnce(1, this, this.setupList)
        // this.setupList();
    }


    runShowAnim() {
        SoundMgr.Ins.playEffect(SoundType.SHOW_DIALOG);
        this.root.scale(0, 0);
        Laya.Tween.to(this.root, { scaleX: 1, scaleY: 1 }, 700, Laya.Ease.sineOut);
    }

    setupList() {

        this.list.itemRender = ui.moduleView.shop.ShopItemViewUI;
        let datas: any = [];
        Object.assign(datas, GlobalData.getAllValue(GlobalData.HeroConfigVO));
        // console.log(Game.hero.playerData.heros[10001])
        for (let i = 0; i < datas.length; i++) {
            // console.log(Game.hero.playerData.heros[datas[i].id])
            if (Game.hero.playerData.heros.indexOf(datas[i].id) !== -1) {
                datas[i].owned = true;
            }
        }

        this.list.array = datas;

        this.list.repeatY = Math.round(datas.length / 2);

        this.list.vScrollBarSkin = "";

        this.list.selectEnable = true;
        this.list.renderHandler = Laya.Handler.create(this, this.onListRender, null, false);

 
    }

    onListRender(cell: ui.moduleView.shop.ShopItemViewUI, index: number): void {
        cell.btn.skin = "images/shop/shop_item_border.png";
        if (cell.dataSource.owned && cell.dataSource.owned === true) {
            cell.titleRoot.visible = true;
            cell.title.value = "" + cell.dataSource.name;
            cell.bg.skin = "images/shop/shop_item_owned.png";
            cell.icon.visible = true;
            cell.icon.skin = "images/shop/hero_icon" + cell.dataSource.id + ".png";
            if (cell.dataSource.id === Game.hero.playerData.heroId) {
                cell.btn.skin = "images/shop/shop_item_border_select.png";
            }
        }
        else {
            cell.bg.skin = "images/shop/shop_item_empty.png";
            cell.icon.visible = false;
            cell.titleRoot.visible = false;
        }
    }

    clickClose() {
        SoundMgr.Ins.playEffect(SoundType.CLICK);
        this.removeSelf();
    }

    ClickBuy() {
        if(Game.hero.playerData.diamond>=Game.gameConfigVO.heroBuyCost){
            
        }
    }




}